<?php

use think\migration\Migrator;

class DocumentDocument extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected string $name = 'document';

    public function up(): void
    {
        /**
         * CREATE TABLE `test_document` (
         * `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
         * `type` varchar(20) NOT NULL DEFAULT '' COMMENT '类型',
         * `sub_type` varchar(20) DEFAULT '' COMMENT '子类型',
         * `title` varchar(100) NOT NULL DEFAULT '' COMMENT '标题',
         * `content` text NOT NULL COMMENT '内容',
         * `status` tinyint(1) NOT NULL COMMENT '状态',
         * `create_at` datetime NOT NULL COMMENT '添加时间',
         * `update_at` datetime NOT NULL COMMENT '新建时间',
         * PRIMARY KEY (`id`),
         * KEY `type` (`type`,`sub_type`,`status`,`create_at`)
         * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文档';
         */

        $table = $this->table($this->name, [
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'comment'     => '文档管理',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id']
        ]);

        $table->addColumn('type', 'string', ['limit' => 20, 'default' => '', 'null' => false, 'comment' => '类型'])
            ->addColumn('sub_type', 'string', ['limit' => 20, 'default' => '', 'null' => false, 'comment' => '子类型'])
            ->addColumn('title', 'string', ['limit' => 100, 'default' => '', 'null' => false, 'comment' => '标题'])
            ->addColumn('content', 'text', ['null' => false, 'comment' => '内容'])
            ->addColumn('status', 'boolean', ['default' => 1, 'null' => false, 'comment' => '状态'])
            ->addColumn('create_at', 'datetime', ['null' => false, 'comment' => '添加时间'])
            ->addColumn('update_at', 'datetime', ['null' => false, 'comment' => '新建时间'])
            ->addIndex(['type', 'sub_type', 'status', 'create_at'])
            ->create();
    }

    public function down(): void
    {
        // 删除表
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
